本文整理汇总了C#中ServiceStack.Auth.AuthUserSession类的典型用法代码示例。如果您正苦于以下问题:C# AuthUserSession类的具体用法?C# AuthUserSession怎么用?C# AuthUserSession使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。
AuthUserSession类属于ServiceStack.Auth命名空间,在下文中一共展示了AuthUserSession类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: LoadUserAuthInfo
protected override void LoadUserAuthInfo(AuthUserSession userSession, IAuthTokens tokens, Dictionary<string, string> authInfo)
{
if (authInfo.ContainsKey("user_id"))
tokens.UserId = authInfo.GetValueOrDefault("user_id");
if (authInfo.ContainsKey("screen_name"))
tokens.UserName = authInfo.GetValueOrDefault("screen_name");
try
{
if (tokens.UserId != null)
{
var oauthToken = new OAuthAccessToken
{
OAuthProvider = this,
AccessToken = tokens.AccessToken,
AccessTokenSecret = tokens.AccessTokenSecret,
};
var json = AuthHttpGateway.DownloadTwitterUserInfo(oauthToken, tokens.UserId);
var objs = JsonObject.ParseArray(json);
if (objs.Count > 0)
{
var obj = objs[0];
tokens.DisplayName = obj.Get("name");
string profileUrl;
if (obj.TryGetValue("profile_image_url", out profileUrl))
tokens.Items[AuthMetadataProvider.ProfileUrlKey] = profileUrl;
if (SaveExtendedUserInfo)
{
obj.Each(x => authInfo[x.Key] = x.Value);
}
}
}
}
catch (Exception ex)
{
Log.Error($"Could not retrieve twitter user info for '{userSession.TwitterUserId}'", ex);
}
LoadUserOAuthProvider(userSession, tokens);
}
示例2: LoadUserAuthInfo
protected override void LoadUserAuthInfo(AuthUserSession userSession, IAuthTokens tokens, System.Collections.Generic.Dictionary<string, string> authInfo)
{
try
{
var json = AuthHttpGateway.DownloadFacebookUserInfo(tokens.AccessTokenSecret);
var obj = JsonObject.Parse(json);
tokens.UserId = obj.Get("id");
tokens.UserName = obj.Get("username");
tokens.DisplayName = obj.Get("name");
tokens.FirstName = obj.Get("first_name");
tokens.LastName = obj.Get("last_name");
tokens.Email = obj.Get("email");
LoadUserOAuthProvider(userSession, tokens);
}
catch (Exception ex)
{
Log.Error("Could not retrieve facebook user info for '{0}'".Fmt(tokens.DisplayName), ex);
}
}
示例3: LoadUserAuthInfo
protected override void LoadUserAuthInfo(AuthUserSession userSession, IAuthTokens tokens, System.Collections.Generic.Dictionary<string, string> authInfo)
{
try
{
var json = AuthHttpGateway.DownloadFacebookUserInfo(tokens.AccessTokenSecret);
var obj = JsonObject.Parse(json);
tokens.UserId = obj.Get("id");
tokens.UserName = obj.Get("username");
tokens.DisplayName = obj.Get("name");
tokens.FirstName = obj.Get("first_name");
tokens.LastName = obj.Get("last_name");
tokens.Email = obj.Get("email");
if (SaveExtendedUserInfo)
{
obj.Each(x => authInfo[x.Key] = x.Value);
}
json = AuthHttpGateway.DownloadFacebookUserInfo(tokens.AccessTokenSecret, "picture");
obj = JsonObject.Parse(json);
var picture = obj.Object("picture");
var data = picture != null ? picture.Object("data") : null;
if (data != null)
{
string profileUrl;
if (data.TryGetValue("url", out profileUrl))
tokens.Items[AuthMetadataProvider.ProfileUrlKey] = profileUrl;
}
}
catch (Exception ex)
{
Log.Error("Could not retrieve facebook user info for '{0}'".Fmt(tokens.DisplayName), ex);
}
LoadUserOAuthProvider(userSession, tokens);
}
示例4: GetRegistrationService
public static RegisterService GetRegistrationService(
IUserAuthRepository userAuthRepository,
AuthUserSession oAuthUserSession = null,
BasicRequest request = null)
{
if (request == null)
request = new BasicRequest();
if (oAuthUserSession == null)
oAuthUserSession = request.ReloadSession();
oAuthUserSession.Id = request.Response.CreateSessionId(request);
request.Items[ServiceExtensions.RequestItemsSessionKey] = oAuthUserSession;
var mockAppHost = new BasicAppHost();
mockAppHost.Container.Register<IAuthRepository>(userAuthRepository);
var authService = new AuthenticateService {
Request = request,
};
authService.SetResolver(mockAppHost);
mockAppHost.Register(authService);
var registrationService = new RegisterService {
AuthRepo = userAuthRepository,
Request = request,
RegistrationValidator =
new RegistrationValidator { UserAuthRepo = RegistrationServiceTests.GetStubRepo() },
};
registrationService.SetResolver(mockAppHost);
return registrationService;
}
示例5: ShouldSetReferrerFromRedirectParam
public void ShouldSetReferrerFromRedirectParam()
{
using (TestAppHost())
{
var request = new MockHttpRequest("myapp", "GET", "text", "/myapp", new NameValueCollection {
{"redirect", "http://localhost/myapp/secure-resource"}
}, Stream.Null, null);
var mockAuthService = MockAuthService(request);
var session = new AuthUserSession();
Subject.Authenticate(mockAuthService.Object, session, new Authenticate());
session.ReferrerUrl.Should().Be("http://localhost/myapp/secure-resource");
}
}
示例6: LoadUserAuthInfo
protected override void LoadUserAuthInfo(AuthUserSession userSession, IAuthTokens tokens, Dictionary<string, string> authInfo)
{
try
{
//sig = md5( request_params_composed_string + md5(access_token + application_secret_key) )
string innerSignature = Encoding.UTF8.GetBytes(tokens.AccessTokenSecret + ConsumerSecret).ToMd5Hash();
string signature = Encoding.UTF8.GetBytes("application_key={0}".Fmt(PublicKey) + innerSignature).ToMd5Hash();
string payload = "access_token={0}&sig={1}&application_key={2}".Fmt(tokens.AccessTokenSecret, signature, PublicKey);
string json = "http://api.odnoklassniki.ru/api/users/getCurrentUser".PostToUrl(payload, "*/*", RequestFilter);
JsonObject obj = JsonObject.Parse(json);
if (!obj.Get("error").IsNullOrEmpty())
{
Log.Error("Could not retrieve Odnoklassniki user info for '{0}', Response:{1}".Fmt(tokens.DisplayName, json));
return;
}
//response fields info: http://apiok.ru/wiki/display/api/users.getCurrentUser+ru
var location = JsonObject.Parse(obj.GetUnescaped("location"));
tokens.UserId = obj.Get("uid");
tokens.DisplayName = obj.Get("name");
tokens.FirstName = obj.Get("first_name");
tokens.LastName = obj.Get("last_name");
tokens.BirthDateRaw = obj.Get("birthday");
tokens.Language = obj.Get("locale");
tokens.Country = location.Get("countryCode");
tokens.City = location.Get("city");
tokens.Gender = obj.Get("gender");
LoadUserOAuthProvider(userSession, tokens);
}
catch (Exception ex)
{
Log.Error("Could not retrieve Odnoklassniki user info for '{0}'".Fmt(tokens.DisplayName), ex);
}
}
示例7: ShouldAbortIfStateValuesDoNotMatch
public void ShouldAbortIfStateValuesDoNotMatch()
{
// If the state value in the response matches the state value in the request,
// the application should store the authorization code for use in the access token request.
using (TestAppHost())
{
Subject.ClientId = "2d4d11a2-f814-46a7-890a-274a72a7309e";
Subject.CallbackUrl = "http://localhost/myapp/";
var request = new MockHttpRequest("myapp", "GET", "text", "/myapp", new NameValueCollection {
{"code", "code123"},
{"session_state", "dontcare"},
{"state", "state123" }
}, Stream.Null, null);
var mockAuthService = MockAuthService(request);
using (new HttpResultsFilter
{
StringResultFn = tokenRequest =>
{
Assert.Fail("Should never have made token request since the state was not matched");
return @"{
""access_token"": ""fake token"",
""id_token"": ""eyJ0eXAiOiJKV1QiLCJhbGciOiJub25lIn0.eyJhdWQiOiIyZDRkMTFhMi1mODE0LTQ2YTctODkwYS0yNzRhNzJhNzMwOWUiLCJpc3MiOiJodHRwczovL3N0cy53aW5kb3dzLm5ldC83ZmU4MTQ0Ny1kYTU3LTQzODUtYmVjYi02ZGU1N2YyMTQ3N2UvIiwiaWF0IjoxMzg4NDQwODYzLCJuYmYiOjEzODg0NDA4NjMsImV4cCI6MTM4ODQ0NDc2MywidmVyIjoiMS4wIiwidGlkIjoiN2ZlODE0NDctZGE1Ny00Mzg1LWJlY2ItNmRlNTdmMjE0NzdlIiwib2lkIjoiNjgzODlhZTItNjJmYS00YjE4LTkxZmUtNTNkZDEwOWQ3NGY1IiwidXBuIjoiZnJhbmttQGNvbnRvc28uY29tIiwidW5pcXVlX25hbWUiOiJmcmFua21AY29udG9zby5jb20iLCJzdWIiOiJKV3ZZZENXUGhobHBTMVpzZjd5WVV4U2hVd3RVbTV5elBtd18talgzZkhZIiwiZmFtaWx5X25hbWUiOiJNaWxsZXIiLCJnaXZlbl9uYW1lIjoiRnJhbmsifQ.""
}";
}
})
{
var session = new AuthUserSession
{
State = "state133" // Not the same as the state in the request above
};
try { Subject.Authenticate(mockAuthService.Object, session, new Authenticate()); }
catch (UnauthorizedAccessException){}
session.IsAuthenticated.Should().BeFalse("Should not be authenticated");
}
}
}
示例8: VerifyNotAuthenticatedByToken
private void VerifyNotAuthenticatedByToken()
{
Subject.CallbackUrl = "http://localhost/myapp/";
using (TestAppHost())
{
var request = new MockHttpRequest("myapp", "GET", "text", "/myapp", new NameValueCollection
{
{"code", "code123"},
{"state", "D79E5777-702E-4260-9A62-37F75FF22CCE"}
}, Stream.Null, null);
var mockAuthService = MockAuthService(request);
using (new HttpResultsFilter
{
StringResult =
@"{
""access_token"": ""token456"",
""id_token"": ""eyJ0eXAiOiJKV1QiLCJhbGciOiJub25lIn0.eyJhdWQiOiIyZDRkMTFhMi1mODE0LTQ2YTctODkwYS0yNzRhNzJhNzMwOWUiLCJpc3MiOiJodHRwczovL3N0cy53aW5kb3dzLm5ldC83ZmU4MTQ0Ny1kYTU3LTQzODUtYmVjYi02ZGU1N2YyMTQ3N2UvIiwiaWF0IjoxMzg4NDQwODYzLCJuYmYiOjEzODg0NDA4NjMsImV4cCI6MTM4ODQ0NDc2MywidmVyIjoiMS4wIiwidGlkIjoiN2ZlODE0NDctZGE1Ny00Mzg1LWJlY2ItNmRlNTdmMjE0NzdlIiwib2lkIjoiNjgzODlhZTItNjJmYS00YjE4LTkxZmUtNTNkZDEwOWQ3NGY1IiwidXBuIjoiZnJhbmttQGNvbnRvc28uY29tIiwidW5pcXVlX25hbWUiOiJmcmFua21AY29udG9zby5jb20iLCJzdWIiOiJKV3ZZZENXUGhobHBTMVpzZjd5WVV4U2hVd3RVbTV5elBtd18talgzZkhZIiwiZmFtaWx5X25hbWUiOiJNaWxsZXIiLCJnaXZlbl9uYW1lIjoiRnJhbmsifQ.""
}"
})
{
var session = new AuthUserSession();
try{ Subject.Authenticate(mockAuthService.Object, session, new Authenticate()); }
catch (UnauthorizedAccessException){}
session.IsAuthenticated.Should().BeFalse();
}
}
}
示例9: Register
protected object Register(IUserAuthRepository userAuthRepository, AuthUserSession oAuthUserSession, Register register = null)
{
if (register == null)
register = RegisterDto;
var registrationService = GetRegistrationService(userAuthRepository, oAuthUserSession, requestContext);
var response = registrationService.Post(register);
Assert.That(response as IHttpError, Is.Null);
return response;
}
示例10: GetNewSession2
public static AuthUserSession GetNewSession2()
{
var oAuthUserSession = new AuthUserSession();
return oAuthUserSession;
}
示例11: GetRegistrationService
public static RegisterService GetRegistrationService(
IUserAuthRepository userAuthRepository,
AuthUserSession oAuthUserSession = null,
MockRequestContext requestContext = null)
{
if (requestContext == null)
requestContext = new MockRequestContext();
if (oAuthUserSession == null)
oAuthUserSession = requestContext.ReloadSession();
var httpReq = requestContext.Get<IHttpRequest>();
var httpRes = requestContext.Get<IHttpResponse>();
oAuthUserSession.Id = httpRes.CreateSessionId(httpReq);
httpReq.Items[ServiceExtensions.RequestItemsSessionKey] = oAuthUserSession;
var mockAppHost = new BasicAppHost {
Container = requestContext.Container
};
requestContext.Container.Register(userAuthRepository);
var authService = new AuthenticateService {
RequestContext = requestContext,
};
authService.SetResolver(mockAppHost);
mockAppHost.Register(authService);
var registrationService = new RegisterService {
UserAuthRepo = userAuthRepository,
RequestContext = requestContext,
RegistrationValidator =
new RegistrationValidator { UserAuthRepo = RegistrationServiceTests.GetStubRepo() },
};
registrationService.SetResolver(mockAppHost);
return registrationService;
}
示例12: LoadUserAuthInfo
/// <summary>
/// Load the UserAuth info into the session.
/// </summary>
/// <param name="userSession">
/// The User session.
/// </param>
/// <param name="tokens">
/// The OAuth tokens.
/// </param>
/// <param name="authInfo">
/// The auth info.
/// </param>
protected override void LoadUserAuthInfo(AuthUserSession userSession, IAuthTokens tokens, Dictionary<string, string> authInfo)
{
try
{
var contents = AuthHttpGateway.DownloadYammerUserInfo(tokens.UserId);
var authObj = JsonObject.Parse(contents);
tokens.UserId = authObj.Get("id");
tokens.UserName = authObj.Get("name");
tokens.DisplayName = authObj.Get("full_name");
tokens.FullName = authObj.Get("full_name");
tokens.FirstName = authObj.Get("first_name");
tokens.LastName = authObj.Get("last_name");
var emails = authObj.Object("contact").ArrayObjects("email_addresses").ConvertAll(x =>
new EmailAddresses
{
Type = x.Get("type"),
Address = x.Get("address")
});
var email = emails.FirstOrDefault(q => q.Type == "primary");
if (email != null)
{
tokens.Email = email.Address;
}
// Pass along
this.LoadUserOAuthProvider(userSession, tokens);
}
catch (Exception ex)
{
Log.Error("Could not retrieve Yammer user info for '{0}'".Fmt(tokens.DisplayName), ex);
}
}
示例13: LoadUserAuthInfo
protected override void LoadUserAuthInfo(AuthUserSession userSession, IAuthTokens tokens, Dictionary<string, string> authInfo)
{
try
{
string json = "https://login.yandex.ru/info?format=json&oauth_token={0}".Fmt(tokens.AccessTokenSecret).GetJsonFromUrl();
JsonObject obj = JsonObject.Parse(json);
tokens.UserId = obj.Get("id");
tokens.UserName = obj.Get("display_name");
tokens.DisplayName = obj.Get("real_name");
tokens.FirstName = obj.Get("first_name");
tokens.LastName = obj.Get("last_name");
tokens.Email = obj.Get("default_email");
tokens.BirthDateRaw = obj.Get("birthday");
LoadUserOAuthProvider(userSession, tokens);
}
catch (Exception ex)
{
Log.Error("Could not retrieve Yandex user info for '{0}'".Fmt(tokens.DisplayName), ex);
}
}
示例14: LoadUserAuthInfo
protected override void LoadUserAuthInfo(AuthUserSession userSession, IAuthTokens tokens, Dictionary<string, string> authInfo)
{
try
{
tokens.UserId = authInfo["user_id"];
tokens.UserName = authInfo["username"];
tokens.DisplayName = authInfo["name"];
tokens.FirstName = authInfo["first_name"];
tokens.LastName = authInfo["last_name"];
tokens.Email = authInfo["email"];
userSession.UserAuthName = tokens.Email;
this.LoadUserOAuthProvider(userSession, tokens);
}
catch (Exception ex)
{
Log.Error("Could not retrieve Profile info for '{0}'".Fmt(tokens.DisplayName), ex);
}
}
示例15: LoadUserAuthInfo
public void LoadUserAuthInfo(AuthUserSession userSession, IAuthTokens tokens, Dictionary<string, string> authInfo)
{
if (userSession == null)
return;
try
{
using (PrincipalContext pc = new PrincipalContext(ContextType.Domain))
{
var user = UserPrincipal.FindByIdentity(pc, userSession.UserAuthName);
tokens.DisplayName = user.DisplayName;
tokens.Email = user.EmailAddress;
tokens.FirstName = user.GivenName;
tokens.LastName = user.Surname;
tokens.FullName = (String.IsNullOrWhiteSpace(user.MiddleName))
? "{0} {1}".Fmt(user.GivenName, user.Surname)
: "{0} {1} {2}".Fmt(user.GivenName, user.MiddleName, user.Surname);
tokens.PhoneNumber = user.VoiceTelephoneNumber;
}
}
catch (MultipleMatchesException mmex)
{
Log.Error("Multiple windows user info for '{0}'".Fmt(userSession.UserAuthName), mmex);
}
catch (Exception ex)
{
Log.Error("Could not retrieve windows user info for '{0}'".Fmt(tokens.DisplayName), ex);
}
}