Combine the use of authentication both for MVC pages and for Web API pages?

后端 未结 5 1482
面向向阳花
面向向阳花 2020-12-12 21:52

I have an MVC 5 web application and can login with a Login.cshtml page and get a cookie and the login works fine. But, I would like to do a login with the Web API and then (

相关标签:
5条回答
  • 2020-12-12 22:11

    I have similar case with you, but i use a different way to authenticate.

    I have a web and an api, which all for intranet users. I do not use user's identity to pass web and api. Instead, i created a individual web account, and every time web will use this special account to connect to api.

    Because, we also need to make sure that users should not connect to api directly. They should only connect to web ui.

    Hope this help you.

    0 讨论(0)
  • 2020-12-12 22:17

    I assume what you're trying to do is have pages served by MVC have javascript that makes calls to Web API methods. If you're using ASP.NET Identity to handle authentication (which it looks like you're doing), then MVC should be using OAuth tokens that can be passed to Web API for authentication.

    Here's a snippet from some javascript code that works for me in a similar situation:

    var token = sessionStorage.getItem('access_token');
    var headers = {};
    if (token) {
        headers.Authorization = 'Bearer ' + token;
    }
    $.ajax({
        type: <GET/POSt/...>,
        url: <your api>,
        headers: headers
    }).done(function (result, textStatus) {
    
    0 讨论(0)
  • 2020-12-12 22:18

    Ugg... what I had to do was use the Login.cshtml form and override the submit... make an Ajax call to get the WebApi bearer token... and then do the form submit to get the actual MVC cookie. So, I'm actually making two login requests... one for the WebApi token and the other for the MVC cookie.

    Seem pretty hacky to me... it would be nice if there was some way to sign in to MVC using the bearer token... or a call to the WebApi that would return me a cookie that I can use for normal MVC page requests.

    If anyone has a better way I would love to hear it.

    This is script code that I added to Login.cshtml:

        $(document).ready(function () {
            $('form:first').submit(function (e) {
                e.preventDefault();
                var $form = $(this);
                var formData = $form.serializeObject(); // https://github.com/macek/jquery-serialize-object
                formData.grant_type = "password";
                $.ajax({
                    type: "POST",
                    url: '@Url.Content("~/Token")',
                    dataType: "json",
                    data: formData, // seems like the data must be in json format
                    success: function (data) {
                        sessionStorage.setItem('token', data.access_token);
                        $form.get(0).submit(); // do the actual page post now
                    },
                    error: function (textStatus, errorThrown) {
                    }
                });
            });
        });
    
    0 讨论(0)
  • 2020-12-12 22:26

    The best way to achieve this, is to have an authorization server (a Web API generating a token) and token consumption middle-ware in your MVC project. IdentityServer should help. However I have done it like this:

    I built an authorization server using JWT with Web API and ASP.Net Identity as explained here.

    Once you do that, your Web APIs startup.cs will look like this:

     // Configures cookie auth for web apps and JWT for SPA,Mobile apps
     private void ConfigureOAuthTokenGeneration(IAppBuilder app)
     {
        // Configure the db context, user manager and role manager to use a single instance per request
        app.CreatePerOwinContext(ApplicationDbContext.Create);
        app.CreatePerOwinContext<ApplicationUserManager>(ApplicationUserManager.Create);
        app.CreatePerOwinContext<ApplicationRoleManager>(ApplicationRoleManager.Create);
    
        // Cookie for old school MVC application
        var cookieOptions = new CookieAuthenticationOptions
        {
            AuthenticationMode = AuthenticationMode.Active,
            CookieHttpOnly = true, // JavaScript should use the Bearer
            AuthenticationType = DefaultAuthenticationTypes.ApplicationCookie,                
            LoginPath = new PathString("/api/Account/Login"),
            CookieName = "AuthCookie"
        };
        // Plugin the OAuth bearer JSON Web Token tokens generation and Consumption will be here
        app.UseCookieAuthentication(cookieOptions);
    
        OAuthServerOptions = new OAuthAuthorizationServerOptions()
        {
            //For Dev enviroment only (on production should be AllowInsecureHttp = false)
            AllowInsecureHttp = true,
            TokenEndpointPath = new PathString("/oauth/token"),
            AccessTokenExpireTimeSpan = TimeSpan.FromDays(30),
            Provider = new CustomOAuthProvider(),                
            AccessTokenFormat = new CustomJwtFormat(ConfigurationManager.AppSettings["JWTPath"])
        };
    
        // OAuth 2.0 Bearer Access Token Generation
        app.UseOAuthAuthorizationServer(OAuthServerOptions);
    }
    

    You can find the CustomOAuthProvider and CustomJwtFormat classes here.

    I wrote a consumption logic (i.e. middleware) in all my other APIs (Resource servers) that I wanted to secure using the same token. Since you want to consume the token generated by the Web API in your MVC project, after implementing the authorization server, you need to the following:

    In your MVC app, add this in startup.cs:

    public void Configuration(IAppBuilder app)
    {
            ConfigureOAuthTokenConsumption(app);
    }
    
    private void ConfigureOAuthTokenConsumption(IAppBuilder app)
    {
        var issuer = ConfigurationManager.AppSettings["AuthIssuer"];
        string audienceid = ConfigurationManager.AppSettings["AudienceId"];
        byte[] audiencesecret = TextEncodings.Base64Url.Decode(ConfigurationManager.AppSettings["AudienceSecret"]);
    
        app.UseCookieAuthentication(new CookieAuthenticationOptions { CookieName = "AuthCookie" , AuthenticationType=DefaultAuthenticationTypes.ApplicationCookie });
    
        //// Api controllers with an [Authorize] attribute will be validated with JWT
        app.UseJwtBearerAuthentication(
            new JwtBearerAuthenticationOptions
            {
                AuthenticationMode = AuthenticationMode.Passive,
                AuthenticationType = "JWT",
                AllowedAudiences = new[] { audienceid },
                IssuerSecurityTokenProviders = new IIssuerSecurityTokenProvider[]
                {
                    new SymmetricKeyIssuerSecurityTokenProvider(issuer, audiencesecret)                           
                }
    
            });
    }
    

    In your MVC controller, when you receive the token, de-serialize it and generate a cookie from the access token:

    AccessClaims claimsToken = new AccessClaims();
    claimsToken = JsonConvert.DeserializeObject<AccessClaims>(response.Content);
    claimsToken.Cookie = response.Cookies[0].Value;               
    Request.Headers.Add("Authorization", "bearer " + claimsToken.access_token);
    var ctx = Request.GetOwinContext();
    var authenticateResult = await ctx.Authentication.AuthenticateAsync("JWT");
    ctx.Authentication.SignOut("JWT");
    var applicationCookieIdentity = new ClaimsIdentity(authenticateResult.Identity.Claims, DefaultAuthenticationTypes.ApplicationCookie);
    ctx.Authentication.SignIn(applicationCookieIdentity);
    

    Generate a machine key and add it in web.config of your Web API and ASP.Net MVC site.

    With this, a cookie will be created and the [Authorize] attribute in the MVC site and the Web API will honor this cookie.

    P.S. I have done this with a Web API issuing JWT (Authorization server or Auth & resource server) and was able to consume it in an ASP.Net MVC website, SPA Site built in Angular, secure APIs built in python (resource server), spring (resource server) and an Android App.

    0 讨论(0)
  • 2020-12-12 22:26

    From your comments above, from what I understand, you have a scenario wherein you perform login through browser but also have to invoke web-api methods using ajax calls.

    The browser calls are session-cookie based. While the ajax calls from the browser would have the session cookie in the header, what is required is the authentication header to be present for the web-api to perform validation.

    So on a successful login you'd also have to generate a web-api based token, set it as a cookie (that is accessible by javascript) and then while making ajax calls, pick it up from the cookie and include it as header in your 'Authorization' header.

    0 讨论(0)
提交回复
热议问题