これが私がそれをした方法です(おそらく最良のアプローチではありませんが、それは機能します):
- 標準のメンバーシップテーブルを作成します
2.ユーザーをGuidの代わりに通常のIDにリンクする別のテーブルを追加します。これにより、誰かがユーザープロファイルを表示したいときに、URLにGuidを入力する必要がなくなります。また、フィールドDisplayNameがあるため、複数のユーザーが同じDisplayNameを持つことができます。
C#用のOpenIDライブラリを使用する
- 次のコードスニペット(これはまだ完了していませんが、機能します)をアカウントコントローラーに追加します。
[AllowAnonymous]
public ActionResult LoginOpenID(string provider, string returnUrl)
{
using (var openid = new OpenIdRelyingParty())
{
var response = openid.GetResponse();
if (response == null)
{
try
{
var request = openid.CreateRequest(provider);
var fetchRequest = new FetchRequest();
fetchRequest.Attributes.AddRequired(WellKnownAttributes.Contact.Email);
fetchRequest.Attributes.AddRequired(WellKnownAttributes.Name.Alias);
fetchRequest.Attributes.AddRequired(WellKnownAttributes.Name.FullName);
request.AddExtension(fetchRequest);
request.AddCallbackArguments("returnUrl", returnUrl);
return request.RedirectingResponse.AsActionResult();
}
catch (ProtocolException pExp)
{
}
catch (WebException Wexp)
{
}
catch (ArgumentException aexp)
{
}
}
else
{
switch (response.Status)
{
case AuthenticationStatus.Authenticated:
var fetch = response.GetExtension<FetchResponse>();
string alias = fetch.GetAttributeValue(WellKnownAttributes.Name.Alias);
string email = fetch.GetAttributeValue(WellKnownAttributes.Contact.Email);
string fullname = fetch.GetAttributeValue(WellKnownAttributes.Name.FullName);
if (string.IsNullOrEmpty(alias))
alias = response.ClaimedIdentifier;
if (alias.Contains("google"))
{
Random random = new Random();
int randomNumber = random.Next(1000000000);
alias = "user" + randomNumber;
}
if (string.IsNullOrEmpty(email))
email = response.ClaimedIdentifier;
//Now see if the user already exists, if not create them
if (email.Contains("gmail.com") && Membership.FindUsersByEmail(email).Count > 0)
{
var cookie = FormsAuthentication.GetAuthCookie(Membership.GetUserNameByEmail(email), true);
Response.AppendCookie(cookie);
}
else if (Membership.GetUser(response.ClaimedIdentifier) == null && Membership.FindUsersByEmail(email).Count == 0)
{
MembershipCreateStatus membershipCreateStatus;
string password = GetRandomString(6, 9);
MembershipUser user = Membership.CreateUser(response.ClaimedIdentifier.ToString(),
password,
email,
"This is an OpenID account. You should log in with your OpenID.",
GetRandomString(5, 7),
true,
out membershipCreateStatus);
if (membershipCreateStatus != MembershipCreateStatus.Success)
{
TempData["message"] = "Unsuccessful creation of Account. " + membershipCreateStatus.ToString();
return RedirectToAction("Login", "Account");
}
if (membershipCreateStatus == MembershipCreateStatus.Success)
{
user.Comment = alias;
Membership.UpdateUser(user);
using (MyContext context = new MyContext())
{
Data.UserShortId userShortId = new Data.UserShortId { Guid = (Guid)user.ProviderUserKey, DisplayName = alias };
context.UserShortIds.InsertOnSubmit(userShortId);
context.SubmitChanges();
}
}
// Use FormsAuthentication to tell ASP.NET that the user is now logged in,
// with the OpenID Claimed Identifier as their username.
var cookie = FormsAuthentication.GetAuthCookie(response.ClaimedIdentifier, true);
Response.AppendCookie(cookie);
}
else
{
var cookie = FormsAuthentication.GetAuthCookie(response.ClaimedIdentifier, true);
Response.AppendCookie(cookie);
}
break;
case AuthenticationStatus.Canceled:
TempData["message"] = "Login was cancelled at the provider";
return RedirectToAction("Login", "Account");
case AuthenticationStatus.Failed:
TempData["message"] = "Login failed using the provided OpenID identifier";
return RedirectToAction("Login", "Account");
}
}
if (Url.IsLocalUrl(returnUrl))
{
return Redirect(returnUrl);
}
else
{
return RedirectToAction("Index", "Home");
}
}
}
private static Random random = new Random(46258975);
public static int GetRandomInteger(int min, int max)
{
return random.Next(min, max + 1);
}
public static string GetRandomString(int minLength, int maxLength)
{
int strLength = GetRandomInteger(minLength, maxLength);
StringBuilder builder = new StringBuilder();
char ch;
for (int i = 0; i < strLength; i++)
{
ch = Convert.ToChar(Convert.ToInt32(Math.Floor(26 * random.NextDouble() + 65)));
builder.Append(ch);
}
return builder.ToString().ToLower();
}
認証する場合:
@using (Html.BeginForm("LoginOpenId", "Account", FormMethod.Post))
{
@Html.Hidden("returnUrl", Request.QueryString["ReturnUrl"])
<p>Login using:</p>
<input type="submit" class="login-btn facebook" name="provider" value="http://facebook-openid.appspot.com/" />
<input type="submit" class="login-btn google" name="provider" value="https://www.google.com/accounts/o8/id" />
<input type="submit" class="login-btn yahoo" name="provider" value="http://me.yahoo.com/" />
}
ご覧のとおり、これは完了していません。非公式のFB OpenIDプロバイダーを使用していますが、OAuthを使用してFbログインを個別に処理するケースを作成できます。