本文整理汇总了C#中IAuthorizationState类的典型用法代码示例。如果您正苦于以下问题:C# IAuthorizationState类的具体用法?C# IAuthorizationState怎么用?C# IAuthorizationState使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。
IAuthorizationState类属于命名空间,在下文中一共展示了IAuthorizationState类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: ProcessUserAuthorization
/// <summary>
/// Scans the incoming request for an authorization response message.
/// </summary>
/// <param name="actualRedirectUrl">The actual URL of the incoming HTTP request.</param>
/// <param name="authorizationState">The authorization.</param>
/// <returns>The granted authorization, or <c>null</c> if the incoming HTTP request did not contain an authorization server response or authorization was rejected.</returns>
public IAuthorizationState ProcessUserAuthorization(Uri actualRedirectUrl, IAuthorizationState authorizationState = null) {
Contract.Requires<ArgumentNullException>(actualRedirectUrl != null);
if (authorizationState == null) {
authorizationState = new AuthorizationState();
}
var carrier = new HttpRequestInfo("GET", actualRedirectUrl, actualRedirectUrl.PathAndQuery, new System.Net.WebHeaderCollection(), null);
IDirectedProtocolMessage response = this.Channel.ReadFromRequest(carrier);
if (response == null) {
return null;
}
EndUserAuthorizationSuccessAccessTokenResponse accessTokenSuccess;
EndUserAuthorizationSuccessAuthCodeResponse authCodeSuccess;
EndUserAuthorizationFailedResponse failure;
if ((accessTokenSuccess = response as EndUserAuthorizationSuccessAccessTokenResponse) != null) {
UpdateAuthorizationWithResponse(authorizationState, accessTokenSuccess);
} else if ((authCodeSuccess = response as EndUserAuthorizationSuccessAuthCodeResponse) != null) {
this.UpdateAuthorizationWithResponse(authorizationState, authCodeSuccess);
} else if ((failure = response as EndUserAuthorizationFailedResponse) != null) {
authorizationState.Delete();
return null;
}
return authorizationState;
}
示例2: PrepareRequestUserAuthorization
/// <summary>
/// Prepares a request for user authorization from an authorization server.
/// </summary>
/// <param name="authorization">The authorization state to associate with this particular request.</param>
/// <returns>The authorization request.</returns>
public OutgoingWebResponse PrepareRequestUserAuthorization(IAuthorizationState authorization) {
Requires.NotNull(authorization, "authorization");
Requires.ValidState(authorization.Callback != null || (HttpContext.Current != null && HttpContext.Current.Request != null), MessagingStrings.HttpContextRequired);
Requires.ValidState(!string.IsNullOrEmpty(this.ClientIdentifier), OAuth2Strings.RequiredPropertyNotYetPreset, "ClientIdentifier");
Contract.Ensures(Contract.Result<OutgoingWebResponse>() != null);
if (authorization.Callback == null) {
authorization.Callback = this.Channel.GetRequestFromContext().GetPublicFacingUrl()
.StripMessagePartsFromQueryString(this.Channel.MessageDescriptions.Get(typeof(EndUserAuthorizationSuccessResponseBase), Protocol.Default.Version))
.StripMessagePartsFromQueryString(this.Channel.MessageDescriptions.Get(typeof(EndUserAuthorizationFailedResponse), Protocol.Default.Version));
authorization.SaveChanges();
}
var request = new EndUserAuthorizationRequest(this.AuthorizationServer) {
ClientIdentifier = this.ClientIdentifier,
Callback = authorization.Callback,
};
request.Scope.ResetContents(authorization.Scope);
// Mitigate XSRF attacks by including a state value that would be unpredictable between users, but
// verifiable for the same user/session.
// If the host is implementing the authorization tracker though, they're handling this protection themselves.
if (this.AuthorizationTracker == null) {
var context = this.Channel.GetHttpContext();
if (context.Session != null) {
request.ClientState = context.Session.SessionID;
} else {
Logger.OAuth.WarnFormat("No request context discovered, so no client state parameter could be set to mitigate XSRF attacks.");
}
}
return this.Channel.PrepareResponse(request);
}
示例3: Refresh
public RefreshResult Refresh(Uri url, IAuthorizationState authorizationState)
{
var client = GetClient(url);
try
{
var refreshed = client.RefreshAuthorization(authorizationState);
if (!refreshed)
return new RefreshResult
{
Error = "Uhm, not entirely sure what happened."
};
}
catch (Exception e)
{
var error = e.Message;
if (e.InnerException != null)
error = string.Format("{0}\nInner exception: {1}", error, e.InnerException.Message);
return new RefreshResult
{
Error = error
};
}
return new RefreshResult
{
AuthorizationState = authorizationState
};
}
示例4: RequestUserAuthorization
/// <summary>
/// Generates a URL that the user's browser can be directed to in order to authorize
/// this client to access protected data at some resource server.
/// </summary>
/// <param name="authorization">The authorization state that is tracking this particular request. Optional.</param>
/// <param name="implicitResponseType">
/// <c>true</c> to request an access token in the fragment of the response's URL;
/// <c>false</c> to authenticate to the authorization server and acquire the access token (and possibly a refresh token) via a private channel.
/// </param>
/// <param name="state">The client state that should be returned with the authorization response.</param>
/// <returns>
/// A fully-qualified URL suitable to initiate the authorization flow.
/// </returns>
public Uri RequestUserAuthorization(IAuthorizationState authorization, bool implicitResponseType = false, string state = null) {
Requires.NotNull(authorization, "authorization");
Requires.ValidState(!string.IsNullOrEmpty(this.ClientIdentifier));
var request = this.PrepareRequestUserAuthorization(authorization, implicitResponseType, state);
return this.Channel.PrepareResponse(request).GetDirectUriRequest(this.Channel);
}
示例5: Authorize
public void Authorize(ref IAuthorizationState authorization, string refreshToken)
{
if ((authorization == null))
{
authorization = new AuthorizationState
{
Callback = _redirectUri,
RefreshToken = refreshToken
};
}
bool refreshFailed = false;
if (AccessTokenHasToBeRefreshed(authorization))
{
try
{
refreshFailed = !RefreshAuthorization(authorization);
}
catch (ProtocolException)
{
//The refreshtoken is not valid anymore
}
}
if (authorization.AccessToken == null || refreshFailed)
{
using (var loginDialog = new LoginForm(_redirectUri))
{
loginDialog.AuthorizationUri = GetAuthorizationUri(authorization);
loginDialog.ShowDialog();
ProcessUserAuthorization(loginDialog.AuthorizationUri, authorization);
}
}
}
示例6: GetAuthenticatorFromState
/// <summary>
/// Retrieve an IAuthenticator instance using the provided state.
/// </summary>
/// <param name="credentials">OAuth 2.0 credentials to use.</param>
/// <returns>Authenticator using the provided OAuth 2.0 credentials</returns>
public static IAuthenticator GetAuthenticatorFromState(IAuthorizationState credentials)
{
var provider = new StoredStateClient(GoogleAuthenticationServer.Description, CLIENT_ID, CLIENT_SECRET, credentials);
var auth = new OAuth2Authenticator<StoredStateClient>(provider, StoredStateClient.GetState);
auth.LoadAccessToken();
return auth;
}
示例7: GetAuthorization
private IAuthorizationState GetAuthorization(NativeApplicationClient arg)
{
// Get the auth URL:
_state = new AuthorizationState(new[] {DriveService.Scopes.Drive.GetStringValue()});
_state.Callback = new Uri(NativeApplicationClient.OutOfBandCallbackUrl);
Uri authUri = arg.RequestUserAuthorization(_state);
//Show Login UI. It's tip for user
var dlg = new AuthDlg(StorageType.GDrive);
dlg.Top = 0;
dlg.Show();
// Request authorization from the user (by opening a browser window):
//Process.Start(authUri.ToString());
_webViewCallback(authUri.ToString());
dlg.Close(); //close non-modal stub dialog
//open another, modal dialog to block execution until user clicks OK
dlg = new AuthDlg(StorageType.GDrive) {Top = 0};
dlg.ShowDialog();
// Retrieve the access token by using the authorization code:
return arg.ProcessUserAuthorization(dlg.AuthCode, _state);
}
示例8: NamedAuthorizationState
public NamedAuthorizationState(string name, IAuthorizationState authorizationState)
{
Guid = Guid.NewGuid();
Name = name;
AuthorizationState = authorizationState;
IsHistorical = false;
}
示例9: CreateAuthorizingHandler
internal new DelegatingHandler CreateAuthorizingHandler(IAuthorizationState authorization, HttpMessageHandler innerHandler = null)
{
if (authorization == null)
{
throw new Exception("Authorization");
}
return new LinkedInTokenHttpMessageHandler(this, authorization, innerHandler ?? new HttpClientHandler());
}
示例10: BearerTokenHttpMessageHandler
/// <summary>
/// Initializes a new instance of the <see cref="BearerTokenHttpMessageHandler" /> class.
/// </summary>
/// <param name="client">The client associated with the authorization.</param>
/// <param name="authorization">The authorization.</param>
/// <param name="innerHandler">The inner handler.</param>
public BearerTokenHttpMessageHandler(ClientBase client, IAuthorizationState authorization, HttpMessageHandler innerHandler)
: base(innerHandler) {
Requires.NotNull(client, "client");
Requires.NotNull(authorization, "authorization");
Requires.That(!string.IsNullOrEmpty(authorization.AccessToken), "authorization.AccessToken", "AccessToken must be non-empty");
this.Client = client;
this.Authorization = authorization;
}
示例11: CallAPI
private string CallAPI(IAuthorizationState authorization) {
var webClient = new WebClient();
webClient.Headers["Content-Type"] = "application/json";
webClient.Headers["X-JavaScript-User-Agent"] = "Demo";
this.Client.AuthorizeRequest(webClient, this.Authorization);
var valueString = webClient.DownloadString("http://localhost:49810/api/values");
return valueString;
}
示例12: NamedAuthorizationState
public NamedAuthorizationState(string name, IAuthorizationState authorizationState, bool shouldRefresh, Uri url)
{
Guid = Guid.NewGuid();
Name = name;
AuthorizationState = authorizationState;
ShouldRefresh = shouldRefresh;
Url = url;
IsHistorical = false;
}
示例13: Authorize2
internal Authorize2(UserAgentClient client, IAuthorizationState authorizationState) {
Contract.Requires(client != null, "client");
Contract.Requires(authorizationState != null, "authorizationState");
this.InitializeComponent();
this.client = client;
this.Authorization = authorizationState;
Uri authorizationUrl = this.client.RequestUserAuthorization(this.Authorization);
this.webBrowser.Navigate(authorizationUrl.AbsoluteUri); // use AbsoluteUri to workaround bug in WebBrowser that calls Uri.ToString instead of Uri.AbsoluteUri leading to escaping errors.
}
示例14: GetAuthorizationUri
private Uri GetAuthorizationUri(IAuthorizationState authorization)
{
var baseUri = RequestUserAuthorization(authorization);
var authorizationUriBuilder = new UriBuilder(baseUri)
{
Query = baseUri.Query.Substring(1) + "&force_login=1"
};
return authorizationUriBuilder.Uri;
}
示例15: AuthorizeRequest
/// <summary>
/// Adds the OAuth authorization token to an outgoing HTTP request, renewing a
/// (nearly) expired access token if necessary.
/// </summary>
/// <param name="request">The request for protected resources from the service provider.</param>
/// <param name="authorization">The authorization for this request previously obtained via OAuth.</param>
public void AuthorizeRequest(HttpWebRequest request, IAuthorizationState authorization) {
Requires.NotNull(request, "request");
Requires.NotNull(authorization, "authorization");
Requires.True(!string.IsNullOrEmpty(authorization.AccessToken), "authorization");
ErrorUtilities.VerifyProtocol(!authorization.AccessTokenExpirationUtc.HasValue || authorization.AccessTokenExpirationUtc < DateTime.UtcNow || authorization.RefreshToken != null, OAuth2Strings.AuthorizationExpired);
if (authorization.AccessTokenExpirationUtc.HasValue && authorization.AccessTokenExpirationUtc.Value < DateTime.UtcNow) {
ErrorUtilities.VerifyProtocol(authorization.RefreshToken != null, OAuth2Strings.AccessTokenRefreshFailed);
this.RefreshAuthorization(authorization);
}
AuthorizeRequest(request, authorization.AccessToken);
}