Skip to main content
 首页 » 编程设计

c#之用户界面中的 OWIN Web API 授权

2024年02月27日31java哥

我在 MVC 和 Web API 中准备用户界面。通过 OWIN 在 Web API 中的授权是正确的。在登录方法中,您可以看到我只是登录到 Web API 并获取了一些信息 token 等。但是是否可以从 Web API 导入此授权并将其添加到 MVC 中的接口(interface)?如果是怎么办?

例如:mvcUI.User = WebAPI.User

*我还想在 mvcUI 中使用 [授权]、声明

使用的 WebApiAuthorizationHelper 代码:

 public static class WebApiAuthorizationHelper 
    { 
 
        public static string GetToken(string url, string userName, string password) 
        { 
            var pairs = new List<KeyValuePair<string, string>> 
                    { 
                        new KeyValuePair<string, string>( "grant_type", "password" ), 
                        new KeyValuePair<string, string>( "username", userName ), 
                        new KeyValuePair<string, string> ( "Password", password ) 
                    }; 
            var content = new FormUrlEncodedContent(pairs); 
            ServicePointManager.ServerCertificateValidationCallback += (sender, cert, chain, sslPolicyErrors) => true; 
            using (var client = new HttpClient()) 
            { 
                var response = client.PostAsync(url + "Token", content).Result; 
                return response.Content.ReadAsStringAsync().Result; 
            } 
        } 
 
        public static string CallApi(string url, string token) 
        { 
            ServicePointManager.ServerCertificateValidationCallback += (sender, cert, chain, sslPolicyErrors) => true; 
            using (var client = new HttpClient()) 
            { 
                if (!string.IsNullOrWhiteSpace(token)) 
                { 
                    var t = JsonConvert.DeserializeObject<Token>(token); 
 
                    client.DefaultRequestHeaders.Clear(); 
                    client.DefaultRequestHeaders.Add("Authorization", "Bearer " + t.access_token); 
                } 
                var response = client.GetAsync(url).Result; 
                return response.Content.ReadAsStringAsync().Result; 
            } 
        } 
 
        class Token 
        { 
            public string access_token { get; set; } 
            public string token_type { get; set; } 
            public int expires_in { get; set; } 
            public string userName { get; set; } 
            [JsonProperty(".issued")] 
            public string issued { get; set; } 
            [JsonProperty(".expires")] 
            public string expires { get; set; } 
        } 
 
    } 

请您参考如下方法:

您可以在下面的代码中添加信息。

我的 CustomIdentityService

public class CustomIdentityService 
{ 
   protected MyContext _context = new MyContext(); 
 
   public Guid FooInfo(Guid IdentityUserID) 
   { 
    return con.fooTable.Where(x => x.IdentityUserID == IdentityUserID). 
    Select(us => new { us.fooData }).FirstOrDefault().fooData; 
   } 
 } 

Providers/ApplicationOAuthProvider.cs

 public override async Task GrantResourceOwnerCredentials(OAuthGrantResourceOwnerCredentialsContext context) 
    { 
 
    CustomIdentityService _customIdentityService = new CustomIdentityService(); 
 
    ClaimsIdentity oAuthIdentity = await user.GenerateUserIdentityAsync(userManager,OAuthDefaults.AuthenticationType); 
    ClaimsIdentity cookiesIdentity = await user.GenerateUserIdentityAsync(userManager,CookieAuthenticationDefaults.AuthenticationType); 
 
    //Add custom claims code 
    string fooInfo= _customIdentityService.FooInfo(user.Id)); 
    oAuthIdentity.AddClaim(new Claim("fooInfo", fooInfo)); 
    AuthenticationProperties properties = CreateProperties(user.UserName,fooInfo); 
 
    } 
 
  public static AuthenticationProperties CreateProperties(string userName,string fooInfo) 
    { 
       IDictionary<string, string> data = new Dictionary<string, string> 
     { 
        { "fooInfo", fooInfo }, 
        { "userName", userName } 
     }; 
       return new AuthenticationProperties(data); 
     }