mirror of
https://gitee.com/jisol/jisol-game/
synced 2025-09-27 10:46:17 +00:00
提交Unity 联机Pro
This commit is contained in:
@@ -0,0 +1,135 @@
|
||||
#if !BESTHTTP_DISABLE_SIGNALR
|
||||
#if !BESTHTTP_DISABLE_COOKIES && (!UNITY_WEBGL || UNITY_EDITOR)
|
||||
|
||||
using System;
|
||||
|
||||
using BestHTTP.Cookies;
|
||||
using BestHTTP.SignalR.Transports;
|
||||
|
||||
namespace BestHTTP.SignalR.Authentication
|
||||
{
|
||||
public sealed class SampleCookieAuthentication : IAuthenticationProvider
|
||||
{
|
||||
#region Public Properties
|
||||
|
||||
public Uri AuthUri { get; private set; }
|
||||
public string UserName { get; private set; }
|
||||
public string Password { get; private set; }
|
||||
public string UserRoles { get; private set; }
|
||||
|
||||
#endregion
|
||||
|
||||
#region IAuthenticationProvider properties
|
||||
|
||||
public bool IsPreAuthRequired { get; private set; }
|
||||
|
||||
public event OnAuthenticationSuccededDelegate OnAuthenticationSucceded;
|
||||
public event OnAuthenticationFailedDelegate OnAuthenticationFailed;
|
||||
|
||||
#endregion
|
||||
|
||||
#region Privates
|
||||
|
||||
private HTTPRequest AuthRequest;
|
||||
private Cookie Cookie;
|
||||
|
||||
#endregion
|
||||
|
||||
public SampleCookieAuthentication(Uri authUri, string user, string passwd, string roles)
|
||||
{
|
||||
this.AuthUri = authUri;
|
||||
this.UserName = user;
|
||||
this.Password = passwd;
|
||||
this.UserRoles = roles;
|
||||
this.IsPreAuthRequired = true;
|
||||
}
|
||||
|
||||
#region IAuthenticationProvider Implementation
|
||||
|
||||
public void StartAuthentication()
|
||||
{
|
||||
AuthRequest = new HTTPRequest(AuthUri, HTTPMethods.Post, OnAuthRequestFinished);
|
||||
|
||||
// Setup the form
|
||||
AuthRequest.AddField("userName", UserName);
|
||||
AuthRequest.AddField("Password", Password); // not used in the sample
|
||||
AuthRequest.AddField("roles", UserRoles);
|
||||
|
||||
AuthRequest.Send();
|
||||
}
|
||||
|
||||
public void PrepareRequest(HTTPRequest request, RequestTypes type)
|
||||
{
|
||||
// Adding the cookie to the request is not required, as it's managed by the plugin automatically,
|
||||
// but for now, we want to be really sure that it's added
|
||||
request.Cookies.Add(Cookie);
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region Request Handler
|
||||
|
||||
void OnAuthRequestFinished(HTTPRequest req, HTTPResponse resp)
|
||||
{
|
||||
AuthRequest = null;
|
||||
string failReason = string.Empty;
|
||||
|
||||
switch (req.State)
|
||||
{
|
||||
// The request finished without any problem.
|
||||
case HTTPRequestStates.Finished:
|
||||
if (resp.IsSuccess)
|
||||
{
|
||||
Cookie = resp.Cookies != null ? resp.Cookies.Find(c => c.Name.Equals(".ASPXAUTH")) : null;
|
||||
|
||||
if (Cookie != null)
|
||||
{
|
||||
HTTPManager.Logger.Information("CookieAuthentication", "Auth. Cookie found!");
|
||||
|
||||
if (OnAuthenticationSucceded != null)
|
||||
OnAuthenticationSucceded(this);
|
||||
|
||||
// return now, all other paths are authentication failures
|
||||
return;
|
||||
}
|
||||
else
|
||||
HTTPManager.Logger.Warning("CookieAuthentication", failReason = "Auth. Cookie NOT found!");
|
||||
}
|
||||
else
|
||||
HTTPManager.Logger.Warning("CookieAuthentication", failReason = string.Format("Request Finished Successfully, but the server sent an error. Status Code: {0}-{1} Message: {2}",
|
||||
resp.StatusCode,
|
||||
resp.Message,
|
||||
resp.DataAsText));
|
||||
break;
|
||||
|
||||
// The request finished with an unexpected error. The request's Exception property may contain more info about the error.
|
||||
case HTTPRequestStates.Error:
|
||||
HTTPManager.Logger.Warning("CookieAuthentication", failReason = "Request Finished with Error! " + (req.Exception != null ? (req.Exception.Message + "\n" + req.Exception.StackTrace) : "No Exception"));
|
||||
break;
|
||||
|
||||
// The request aborted, initiated by the user.
|
||||
case HTTPRequestStates.Aborted:
|
||||
HTTPManager.Logger.Warning("CookieAuthentication", failReason = "Request Aborted!");
|
||||
break;
|
||||
|
||||
// Connecting to the server is timed out.
|
||||
case HTTPRequestStates.ConnectionTimedOut:
|
||||
HTTPManager.Logger.Error("CookieAuthentication", failReason = "Connection Timed Out!");
|
||||
break;
|
||||
|
||||
// The request didn't finished in the given time.
|
||||
case HTTPRequestStates.TimedOut:
|
||||
HTTPManager.Logger.Error("CookieAuthentication", failReason = "Processing the request Timed Out!");
|
||||
break;
|
||||
}
|
||||
|
||||
if (OnAuthenticationFailed != null)
|
||||
OnAuthenticationFailed(this, failReason);
|
||||
}
|
||||
|
||||
#endregion
|
||||
}
|
||||
}
|
||||
|
||||
#endif
|
||||
#endif
|
@@ -0,0 +1,10 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 48a74a50eeb07bb4ea649a902e9d487a
|
||||
MonoImporter:
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
@@ -0,0 +1,97 @@
|
||||
#if !BESTHTTP_DISABLE_SIGNALR
|
||||
|
||||
namespace BestHTTP.SignalR.Authentication
|
||||
{
|
||||
/// <summary>
|
||||
/// Custom http-header based authenticator.
|
||||
/// <example>
|
||||
/// <code>
|
||||
/// // Server side implementation of the Header-based authenticator
|
||||
/// // Use it by adding the app.Use(typeof(HeaderBasedAuthenticationMiddleware)); line to the Startup class' Configuration function.
|
||||
/// private class HeaderBasedAuthenticationMiddleware : OwinMiddleware
|
||||
/// {
|
||||
/// public HeaderBasedAuthenticationMiddleware(OwinMiddleware next)
|
||||
/// : base(next)
|
||||
/// {
|
||||
/// }
|
||||
///
|
||||
/// public override Task Invoke(IOwinContext context)
|
||||
/// {
|
||||
/// string username = context.Request.Headers.Get("username");
|
||||
/// string roles = context.Request.Headers.Get("roles");
|
||||
///
|
||||
/// if (!String.IsNullOrEmpty(username) && !String.IsNullOrEmpty(roles))
|
||||
/// {
|
||||
/// var identity = new System.Security.Principal.GenericIdentity(username);
|
||||
///
|
||||
/// var principal = new System.Security.Principal.GenericPrincipal(identity, SplitString(roles));
|
||||
///
|
||||
/// context.Request.User = principal;
|
||||
/// }
|
||||
///
|
||||
/// return Next.Invoke(context);
|
||||
/// }
|
||||
///
|
||||
/// private static string[] SplitString(string original)
|
||||
/// {
|
||||
/// if (String.IsNullOrEmpty(original))
|
||||
/// return new string[0];
|
||||
///
|
||||
/// var split = from piece in original.Split(',') let trimmed = piece.Trim() where !String.IsNullOrEmpty(trimmed) select trimmed;
|
||||
///
|
||||
/// return split.ToArray();
|
||||
/// }
|
||||
/// }
|
||||
/// </code>
|
||||
/// </example>
|
||||
/// </summary>
|
||||
class HeaderAuthenticator : IAuthenticationProvider
|
||||
{
|
||||
public string User { get; private set; }
|
||||
public string Roles { get; private set; }
|
||||
|
||||
/// <summary>
|
||||
/// No pre-auth step required for this type of authentication
|
||||
/// </summary>
|
||||
public bool IsPreAuthRequired { get { return false; } }
|
||||
|
||||
#pragma warning disable 0067
|
||||
/// <summary>
|
||||
/// Not used event as IsPreAuthRequired is false
|
||||
/// </summary>
|
||||
public event OnAuthenticationSuccededDelegate OnAuthenticationSucceded;
|
||||
|
||||
/// <summary>
|
||||
/// Not used event as IsPreAuthRequired is false
|
||||
/// </summary>
|
||||
public event OnAuthenticationFailedDelegate OnAuthenticationFailed;
|
||||
|
||||
#pragma warning restore 0067
|
||||
|
||||
/// <summary>
|
||||
/// Constructor to initialise the authenticator with username and roles.
|
||||
/// </summary>
|
||||
public HeaderAuthenticator(string user, string roles)
|
||||
{
|
||||
this.User = user;
|
||||
this.Roles = roles;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Not used as IsPreAuthRequired is false
|
||||
/// </summary>
|
||||
public void StartAuthentication()
|
||||
{ }
|
||||
|
||||
/// <summary>
|
||||
/// Prepares the request by adding two headers to it
|
||||
/// </summary>
|
||||
public void PrepareRequest(BestHTTP.HTTPRequest request, RequestTypes type)
|
||||
{
|
||||
request.SetHeader("username", this.User);
|
||||
request.SetHeader("roles", this.Roles);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#endif
|
@@ -0,0 +1,10 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 5ebd4ce02d369a6498f9be6bb7141ac3
|
||||
MonoImporter:
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
Reference in New Issue
Block a user