The Owin middleware for something third-party login integration into your ASP. NET application is very cool, but I can't seem to figure out how to gouge out the new ID, which replaces the crappy membership API. I am not interested in insisting on the resulting claims and persisting in English and French based data, I just want to do this so I can apply it to my existing project account. I don't want to use the new numbering just to take advantage of these things. I've been browsing the code on CodePlex, but there's a whole bunch of static magic. Can you give me any advice?
This address: codego.net/608804/
--------------------------------------------------------------------------------------------------------------- ----------
1. Use the following code to set up the Owin security middleware:
App. Usecookieauthentication (Newcookieauthenticationoptions{AuthenticationType="Application", Authenticationmode=authenticationmode.passive, Loginpath=NewPathString ("/login"), Logoutpath=NewPathString ("/logout"), App. Setdefaultsigninasauthenticationtype ("External"); app. Usecookieauthentication (Newcookieauthenticationoptions{AuthenticationType="External", Authenticationmode=authenticationmode.passive, CookieName= Cookieauthenticationdefaults.cookieprefix +"External", Expiretimespan= Timespan.fromminutes (5), App. Usegoogleauthentication ();
The above code sets the application's cookie, external cookie and Google external login middleware. The external login middleware converts the login data to the identity and sets it as the middleware for external cookies. In your application, you need to get an external cookie identity and convert it to external login data, then you can use it to check your sample code below. Please use the application's cookie:
varauthentication =System.Web.HttpContext.Current.GetOwinContext (). authentication;varIdentity =NewClaimsidentity ("Application"); identity. Addclaim (NewClaim (Claimtypes.name,"<user name>") ); authentication. Authenticationresponsegrant=NewAuthenticationresponsegrant (Identity,Newauthenticationproperties () {ispersistent=false});
To obtain the application's cookie identity:
var as claimsidentity;
To obtain an external cookie identity (Google):
var authentication = System.Web.HttpContext.Current.GetOwinContext (). authentication; var await authentication. Authenticateasync ("External"); var externalidentity = result. Identity;
Extract external login data from identity:
Public StaticExternallogindata fromidentity (claimsidentity identity) {if(Identity = =NULL) { return NULL; } Claim Providerkeyclaim=identity. FindFirst (Claimtypes.nameidentifier); if(Providerkeyclaim = =NULL||String.IsNullOrEmpty (Providerkeyclaim.issuer)||String.IsNullOrEmpty (Providerkeyclaim.value)) { return NULL; } if(Providerkeyclaim.issuer = =claimsidentity.defaultissuer) {return NULL; } return NewExternallogindata {Loginprovider=Providerkeyclaim.issuer, Providerkey=Providerkeyclaim.value, UserName=identity. Findfirstvalue (Claimtypes.name)};}
How do I ignore identity framework magic, just using Owin validated middleware to get the requirements I seek?