本文整理汇总了C#中IAuthentication.Authenticate方法的典型用法代码示例。如果您正苦于以下问题:C# IAuthentication.Authenticate方法的具体用法?C# IAuthentication.Authenticate怎么用?C# IAuthentication.Authenticate使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类IAuthentication
的用法示例。
在下文中一共展示了IAuthentication.Authenticate方法的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: LogOn
/// <summary>
/// Logs on with specified authentication method.
/// </summary>
/// <param name="authentication">The authentication method.</param>
/// <returns></returns>
/// <exception cref="System.InvalidOperationException">
/// Already logged on
/// or
/// Another unfinished log on/off request exists.
/// </exception>
/// <exception cref="System.Exception">Null user info is not allowed</exception>
public async Task LogOn(IAuthentication authentication)
{
if (UserInfo != null)
{
throw new InvalidOperationException("Already logged on");
}
if (Interlocked.CompareExchange(ref IsWorking, 1, 0) == 0)
{
try
{
var result = await authentication.Authenticate();
if (result == null)
{
throw new Exception("Null user info is not allowed");
}
UserInfo = result;
ServerConnection.AccessToken = UserInfo.AccessToken;
return;
}
finally
{
Interlocked.CompareExchange(ref IsWorking, 0, 1);
}
}
throw new InvalidOperationException("Another unfinished log on/off request exists.");
}
示例2: Login
public static Credential Login(IAuthentication authentication, ICredentialProvider credentialProvider, string identity, string password, string @namespace, bool isRemember, out string redirectUrl)
{
if(authentication == null)
throw new ArgumentNullException("authentication");
if(credentialProvider == null)
throw new ArgumentNullException("credentialProvider");
//进行身份验证(即验证身份标识和密码是否匹配)
var result = authentication.Authenticate(identity, password, @namespace);
//注册用户凭证
var credential = credentialProvider.Register(result.User, AuthenticationUtility.GetScene(), (result.HasExtendedProperties ? result.ExtendedProperties : null));
//将注册成功的用户凭证保存到Cookie中
AuthenticationUtility.SetCredentialCookie(credential, isRemember ? TimeSpan.FromDays(7) : TimeSpan.Zero);
object redirectObject = null;
//如果验证事件中显式指定了返回的URL,则使用它所指定的值
if(result.HasExtendedProperties && result.ExtendedProperties.TryGetValue("RedirectUrl", out redirectObject) && redirectObject != null)
redirectUrl = redirectObject.ToString();
else //返回重定向的路径中
redirectUrl = AuthenticationUtility.GetRedirectUrl(credential.Scene);
return credential;
}